#include <shlobj.h>
#include <string_view>
#include "include_shell_header.h"

namespace elibshell {
	BOOL CreateLink(const std::string_view& LinkName, const std::string_view& FilePath, const std::string_view& remark, DWORD wHotkey, const std::string_view& comline, const std::string_view& workdir)
	{
		if (LinkName.empty() || FilePath.empty())
			return FALSE;

		BOOL blSucceeded = FALSE;
		IShellLinkA* piShellLink;
		if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&piShellLink)))
		{
			IPersistFile* piPersistFile;
			if (SUCCEEDED(piShellLink->QueryInterface(IID_IPersistFile, (void**)&piPersistFile)))
			{
				piShellLink->SetPath(FilePath.data());
				piShellLink->SetShowCmd(SW_SHOW);
				if (!remark.empty())  piShellLink->SetDescription(remark.data());
				if (!comline.empty())  piShellLink->SetArguments(comline.data());
				if (wHotkey != 0)  piShellLink->SetHotkey((WORD)wHotkey);
				if (!workdir.empty())  piShellLink->SetWorkingDirectory(workdir.data());
				std::string FileNmae = (strchr(LinkName.data(), '.') != NULL ? std::string(LinkName) : (std::string(LinkName) + ".lnk"));
				WCHAR WideCharStr[MAX_PATH] = { 0 };
				MultiByteToWideChar(0, 0, FileNmae.c_str(), -1, WideCharStr, MAX_PATH);
				blSucceeded = SUCCEEDED(piPersistFile->Save(WideCharStr, TRUE));
				piPersistFile->Release();
			}
			piShellLink->Release();
		}
		return blSucceeded;
	}
}


